/*
* @Author: majingyu
* @Date:   2017-04-25 15:59:50
* @Last Modified by:   jingyu.ma
* @Last Modified time: 2017-08-12 18:30:39
*/

import { getParams } from 'common/getParams';
import Fetch from 'common/fetch';
import { toast,confirm } from 'common/tips';

const CHANGE_NOWPAGE_WITH_HEADER_STATE = 'CHANGE_NOWPAGE_WITH_HEADER_STATE';

export const changeNowPageWithHeaderState = (mainNowPage) => {
    return {
        type:CHANGE_NOWPAGE_WITH_HEADER_STATE,
        mainNowPage
    }
}

const CHANGE_SHOW_STATUS  = 'CHANGE_SHOW_STATUS';

export const changeShowStatus = (status) => {
    return {
        type: CHANGE_SHOW_STATUS,
        status
    }
}
//添加用户手机号
export const addUserMobile = (params) => (dispatch, getState) =>{
    //dispatch(publish(SUBMIT_SKILL_BEGIN));
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    return fetch(`wx/user/fillInMobile?mobile=`+params.mobile).then(res=>res.json()).then(data=>{
        if(data.status == 200){
            toast('成功添加')
            setTimeout(()=>{
            },1000);
        }else{
            toast(data.err)
            //dispatch(publish(SUBMIT_SKILL_ERROR,data))
        }
    }).catch(e=>{
        toast('发生网络错误.')
        dispatch(publish(SUBMIT_SKILL_ERROR,e))
    })
}

//获取城市列表
const getcityType = (type, data) => {
    let action = {
        type:type
    }
    if(type == GET_CITY_LIST_SUCCESS){
        action['data'] = data;
    }else if(type == GET_CITY_LIST_ERROR){
        action['e'] = data;
    }else if(type == GET_CITY_LIST_BIGIN){
        action['data'] = data;
    }
    return action;
}
const GET_CITY_LIST_BIGIN = 'GET_CITY_LIST_BIGIN';
const GET_CITY_LIST_SUCCESS = 'GET_CITY_LIST_SUCCESS';
const GET_CITY_LIST_ERROR = 'GET_CITY_LIST_ERROR';

export const getCitiesList = () => (dispatch, getState) => {
    dispatch(getcityType(GET_CITY_LIST_BIGIN));
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('wx/city/allCity').then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(getcityType(GET_CITY_LIST_SUCCESS,data.data));
        }else{
            message.error(data.err)
            dispatch(getcityType(GET_CITY_LIST_ERROR,data.err));
        }
    }).catch(e=>{
        dispatch(getcityType(GET_CITY_LIST_ERROR,e));
    })
}

//修改当前城市
const editCityType = (type, data) => {
    let action = {
        type:type
    }
    if(type == EDIT_CITY_SUCCESS){
        action['data'] = data;
    }else if(type == EDIT_CITY_ERROR){
        action['e'] = data;
    }else if(type == EDIT_CITY_BIGIN){
        action['data'] = data;
    }
    return action;
}
const EDIT_CITY_BIGIN = 'EDIT_CITY_BIGIN';
const EDIT_CITY_SUCCESS = 'EDIT_CITY_SUCCESS';
const EDIT_CITY_ERROR = 'EDIT_CITY_ERROR';

export const editCity = (param) => (dispatch, getState) => {
    dispatch(getcityType(EDIT_CITY_BIGIN));
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/web/app/editCity',{
        method:'POST',
        body:JSON.stringify(param),
        credentials:'include',
        headers:myHeaders
    }).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(getcityType(EDIT_CITY_SUCCESS,data));
        }else{
            message.error(data.err)
            dispatch(getcityType(EDIT_CITY_ERROR,data.err));
        }
    }).catch(e=>{
        dispatch(getcityType(EDIT_CITY_ERROR,e));
    })
}

//获取当前城市
const getMyCityType = (type, data) => {
    let action = {
        type:type
    }
    if(type == GET_CITY_SUCCESS){
        action['data'] = data;
    }else if(type == GET_CITY_ERROR){
        action['e'] = data;
    }
    return action;
}
const GET_CITY_BIGIN = 'GET_CITY_BIGIN';
const GET_CITY_SUCCESS = 'GET_CITY_SUCCESS';
const GET_CITY_ERROR = 'GET_CITY_ERROR';

export const getCity = (param) => (dispatch, getState) => {
    dispatch(getMyCityType(GET_CITY_BIGIN));
    let paramsList = getParams(param);
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/wx/city/positioning?'+paramsList).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(getMyCityType(GET_CITY_SUCCESS,data));
        }else{
            message.error(data.err)
            dispatch(getMyCityType(GET_CITY_ERROR,data.err));
        }
    }).catch(e=>{
        dispatch(getMyCityType(GET_CITY_ERROR,e));
    })
}